package org.mcjug;

import org.mcjug.model.UpdateService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

public class NetworkReceiver extends BroadcastReceiver {
	private static final String TAG = NetworkReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(TAG, "onReceive: Got intent with action: " + intent.getAction());
		if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			boolean isNetworkDown = intent.getBooleanExtra(
					ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
			if (isNetworkDown) {
				Log.d(TAG, "onReceive: disconnected, stopping UpdaterService");
				context.stopService(new Intent(context, UpdateService.class));
			} else {
				Log.d(TAG, "onReceive: connected, starting UpdaterService");
				context.startService(new Intent(context, UpdateService.class));
			}
		} else {
			context.startService(new Intent(context, UpdateService.class));
		}
	}

}
